﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[AddComponentMenu("Game/Enemy")]
public class Enemy : MonoBehaviour {

    public float m_speed = 1.0f;
    public float life = 10;

    float m_rotateSpeed = 30;
    internal Renderer renderer;
    internal bool m_isActive = false;
    public Transform m_explosionFX;

    // Use this for initialization
    void Start () {
        renderer = GetComponent<Renderer>();
	}
	
	// Update is called once per frame
	void Update () {
        UpdateMove();

        if (m_isActive && !renderer.isVisible)
        {
            Destroy(gameObject);
        }
    }

    private void OnBecameVisible()
    {
        m_isActive = true;
    }

    protected virtual void UpdateMove()
    {
        float rotate = Mathf.Sin(Time.time) * Time.deltaTime;
        transform.Translate(new Vector3(rotate, 0,-m_speed*Time.deltaTime));
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag=="PlayRocket")
        {
            rocket m_rocket = other.GetComponent<rocket>();
            if (m_rocket!=null)
            {
                life -= m_rocket.m_power;
                if (life<=0)
                {
                    Manager.Instance.addScore(1);
                    Instantiate(m_explosionFX, transform.position, Quaternion.identity);
                    Destroy(gameObject);

                }

            }
        }
        else if(other.tag=="Player")
        {
            life = 0;
            Instantiate(m_explosionFX, transform.position, Quaternion.identity);
            Destroy(gameObject);
        }

    }
}
